/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package StackModel.Stack.impl;

import StackModel.Stack.Stack;
import StackModel.Stack.StackPackage;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EDataTypeEList;

/**
 * <!-- begin-user-doc --> An implementation of the model object '
 * <em><b>Stack</b></em>'. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 * <li>{@link StackModel.Stack.impl.StackImpl#getChildren <em>Children</em>}</li>
 * <li>{@link StackModel.Stack.impl.StackImpl#getMaxCnt <em>Max Cnt</em>}</li>
 * </ul>
 * </p>
 * 
 * @generated
 */
public class StackImpl extends EObjectImpl implements Stack {
  /**
   * The cached value of the '{@link #getChildren() <em>Children</em>}'
   * attribute list. <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @see #getChildren()
   * @generated
   * @ordered
   */
  protected EList<Object> children;

  /**
   * The default value of the '{@link #getMaxCnt() <em>Max Cnt</em>}' attribute.
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @see #getMaxCnt()
   * @generated
   * @ordered
   */
  protected static final Integer MAX_CNT_EDEFAULT = null;

  /**
   * The cached value of the '{@link #getMaxCnt() <em>Max Cnt</em>}' attribute.
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @see #getMaxCnt()
   * @generated
   * @ordered
   */
  protected Integer maxCnt = MAX_CNT_EDEFAULT;

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  protected StackImpl() {
    super();
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  protected EClass eStaticClass() {
    return StackPackage.Literals.STACK;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public EList<Object> getChildren() {
    if (children == null) {
      children =
          new EDataTypeEList<Object>(Object.class, this,
              StackPackage.STACK__CHILDREN);
    }
    return children;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public Integer getMaxCnt() {
    return maxCnt;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public void setMaxCnt(Integer newMaxCnt) {
    Integer oldMaxCnt = maxCnt;
    maxCnt = newMaxCnt;
    if (eNotificationRequired())
      eNotify(new ENotificationImpl(this, Notification.SET,
          StackPackage.STACK__MAX_CNT, oldMaxCnt, maxCnt));
  }

  public void push(Object e) {
    if (children.size() < this.maxCnt-1) {
      children.add(e);
    }
  }

  public Object pop() {
    if (children.size() >= 0)
      return children.remove(children.size());
    else
      throw new UnsupportedOperationException();
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public Object eGet(int featureID, boolean resolve, boolean coreType) {
    switch (featureID) {
    case StackPackage.STACK__CHILDREN:
      return getChildren();
    case StackPackage.STACK__MAX_CNT:
      return getMaxCnt();
    }
    return super.eGet(featureID, resolve, coreType);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @SuppressWarnings("unchecked")
  @Override
  public void eSet(int featureID, Object newValue) {
    switch (featureID) {
    case StackPackage.STACK__CHILDREN:
      getChildren().clear();
      getChildren().addAll((Collection<? extends Object>) newValue);
      return;
    case StackPackage.STACK__MAX_CNT:
      setMaxCnt((Integer) newValue);
      return;
    }
    super.eSet(featureID, newValue);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public void eUnset(int featureID) {
    switch (featureID) {
    case StackPackage.STACK__CHILDREN:
      getChildren().clear();
      return;
    case StackPackage.STACK__MAX_CNT:
      setMaxCnt(MAX_CNT_EDEFAULT);
      return;
    }
    super.eUnset(featureID);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public boolean eIsSet(int featureID) {
    switch (featureID) {
    case StackPackage.STACK__CHILDREN:
      return children != null && !children.isEmpty();
    case StackPackage.STACK__MAX_CNT:
      return MAX_CNT_EDEFAULT == null ? maxCnt != null : !MAX_CNT_EDEFAULT
          .equals(maxCnt);
    }
    return super.eIsSet(featureID);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public String toString() {
    if (eIsProxy()) return super.toString();

    StringBuffer result = new StringBuffer(super.toString());
    result.append(" (children: ");
    result.append(children);
    result.append(", maxCnt: ");
    result.append(maxCnt);
    result.append(')');
    return result.toString();
  }

} // StackImpl
